package com.harvest.scientific.ladder.util

import android.content.res.Configuration
import com.harvest.scientific.ladder.base.ApGlobals
import java.util.Locale


object ApUtil {

    fun switchLanguage(languageCode: String) {
        val context = ApGlobals.get()
        val resources = context.resources
        val dm = resources.displayMetrics
        val config: Configuration = resources.configuration
        when(languageCode) {
            "zh-rCN" -> {
                config.locale = Locale("zh-rCN")
            }
            "fr" -> {
                config.locale = Locale("fr")
            }
            else -> {
                config.locale = Locale("fr")
            }
        }
        resources.updateConfiguration(config, dm)
    }

    @JvmStatic
    fun getVersionName(): String {
        runCatching {
            val context = ApGlobals.get()
            val pm = context.packageManager
            val packageInfo = pm.getPackageInfo(context.packageName, 0)
            return packageInfo.versionName
        }.onFailure {
        }
        return "1.0.0"
    }

    @JvmStatic
    fun getVersionCode(): String {
        runCatching {
            val context = ApGlobals.get()
            val pm = context.packageManager
            val packageInfo = pm.getPackageInfo(context.packageName, 0)
            return packageInfo.versionCode.toString()
        }.onFailure {  }
        return "100"
    }
}